package org.club.Controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.club.Service.AnnouncementService;
import org.club.common.model.dto.Announcemen;
import org.club.common.model.dto.AnnouncementsDto;
import org.club.common.model.entity.Announcement;
import org.club.common.util.ApiResponseMe;
import org.club.common.model.vo.AnnouncementUserVO;
import org.club.common.model.vo.AnnouncementVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告控制器类，用于处理与公告相关的HTTP请求
 */
@Tag(name = "公告创建", description = "创建公告，获取已发布，根据id获取公告")
@RestController
@Slf4j
@RequestMapping("/admin")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    /**
     * 获取所有公告信息
     *
     * @param announcementsDto 公告查询条件DTO
     * @return 包含公告列表和总数的响应结果
     */
    @PostMapping("/announcements")
    public ApiResponseMe getAllAnnouncements(@RequestBody AnnouncementsDto announcementsDto) {
        // 调用服务层方法获取公告列表
        List<AnnouncementVO> announcements = announcementService.getAllAnnouncements(announcementsDto);
        // 检查返回的公告列表是否为空
        if (announcements == null) {
            return ApiResponseMe.error(400, "公告列表为空");
        }
        // 创建分页信息
        PageInfo<AnnouncementVO> pageInfo = new PageInfo<>(announcements);
        // 构建返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("total", pageInfo.getTotal());
        result.put("list", announcements);
        return ApiResponseMe.success(result);
    }

    /**
     * 更新公告信息
     *
     * @param announcement 待更新的公告对象
     * @return 更新成功的响应结果
     */
    @PostMapping("/updateAnnouncement")
    public ApiResponseMe updateAnnouncement(@RequestBody Announcement announcement) {
        // 更新公告
        announcementService.updateAnnouncement(announcement);
        return ApiResponseMe.shape(true,"更新成功");
    }

    /**
     * 删除公告
     *
     * @param id 待删除公告的ID
     * @return 删除成功的响应结果
     */
    @DeleteMapping("/deleteAnnouncement")
    public ApiResponseMe deleteAnnouncement(@RequestParam Long id) {
        // 调用服务层方法执行删除公告操作
        announcementService.deleteAnnouncement(id);
        // 如果删除成功返回成功响应
        return ApiResponseMe.shape(true,"删除成功");
    }

    /**
     * 搜索用户，用于公告发布时选择用户
     *
     * @param keyword 搜索关键词
     * @return 包含用户列表的响应结果
     */
    @GetMapping("/searchUsers")
    public ApiResponseMe searchUsers(@RequestParam String keyword) {
        // 根据关键词搜索用户
        List<AnnouncementUserVO> users = announcementService.searchUsers(keyword);
        // 返回成功响应，包含用户列表
        return ApiResponseMe.success(users);
    }

    /**
     * 创建新公告
     *
     * @param announcemen 新公告的封装对象
     * @return 创建成功后的公告ID
     */
    @PostMapping("/createAnnouncement")
    public ApiResponseMe createAnnouncement(@RequestBody Announcemen announcemen) {
        Long id = announcementService.createAnnouncement(announcemen);
        return ApiResponseMe.success(id);
    }
}
